<?php
namespace app\first\controller;

use app\common\controller\GetuiMsg;
use app\first\model\App;
use jswei\push\drive\GeTuiService;
use jswei\push\sdk\geTui\igetui\NotifyInfo_Type;
use jswei\push\sdk\geTui\igetui\template\IGtTransmissionTemplate;
use jswei\push\sdk\geTui\igetui\template\notify\IGtNotify;

/**
 * Class Push
 * @title 消息推送
 * @url /v1/push
 * @desc  消息推送测试接口
 * @version 1.0
 * @package app\first\push
 * @route('v1/push')
 */
class Push extends Base{
    //是否开启授权认证,默认开启
    public $apiAuth = false;
    protected $model = null;
    protected $validate = null;
    /**
    * 需要附加的其他方法,比如:topList,newList
    * protected $extraActionList = ['topList','newList',...];
    * 已经对核心的类进行了重写,扩展的新方法会自动添加到认证中,无需再手动书写
    */
    protected $extraActionList = [];
    /**
    *忽略验证的方法
    */
    protected $skipAuthActionList = ['test'];

    public function __construct(){
        parent::__construct();
        $this->model = new \app\first\model\Push;
        $this->validate = new \app\first\validate\Push;
    }

    /**
     * @title 个推测试推送
     * @method test
     * @param string $client_id 个推ID true 71dd7fae60fa34a170362e857ffd647d
     * @route('v1/push/test','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     */
    public function test($client_id=''){
        $config = config('getui.');
        $push = new GeTuiService($config);
        $payload = [
            'title'=>'测试标题',
            'content'=> '测试内容',
            'payload'=> 'test'
        ];
        $template = $this->_template($payload);
        $push->setTemplate($template);
        $data = $push->sendOne($client_id)->getResult();
        $data['payload'] = $payload;
//        return $this->sendSuccess($data,'发送成功');

        $push = new GetuiMsg();
        $push->SendTouchuanMessageToUniAppList([$client_id],$payload,'','',$out);
        $out['payload'] = $payload;
        return $this->sendSuccess($out);
    }

    /**
     * @param array $payload
     * @param $intent
     * @param string $APPID
     * @param string $APPKEY
     * @return IGtTransmissionTemplate
     */
    public function _template($payload=[],$APPID='',$APPKEY=''){
        $config = config('getui.');
        $APPID = $APPID?$APPID:$config['AppID'];
        $APPKEY = $APPKEY?$APPKEY:$config['AppKey'];
        $intent = 'intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=com.jiexunet/io.dcloud.UNI70C8F6C;S.UP-OL-SU=true;S.title='.$payload['title'].';S.content='.$payload['content'].';S.payload='.json_encode($payload).';end';
        $template = new IGtTransmissionTemplate();
        $template->set_appId($APPID);
        $template->set_appkey($APPKEY);
        $template->set_transmissionType(2);
        $template->set_transmissionContent(json_encode($payload));
        $notify = new IGtNotify();
        $notify->set_title($payload['title']);
        $notify->set_content($payload['content']);
        $notify->set_intent($intent);
        $notify->set_type(NotifyInfo_type::_intent);
        $notify->set_url("https://dev.getui.com/");
        $template->set3rdNotifyInfo($notify);
        return $template;
    }

    /**
     * @title 检测更新
     * @method check_update
     * @route('v1/push/check_update','get')
     * @return \think\facade\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\Xml
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function check_update(){
        $app = new App();
        $info = $app->getLastOne();
        return $this->sendSuccess($info);
    }
}